package com.biostime.qdingding.modules.qd.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.utils.ImageUtils;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;

/**
 * CDN资源控制Controller
 * @author king
 * @version 2015-11-05
 */
@Controller
@RequestMapping(value = "/cdn")
public class CdnController extends BaseController {
	
	@RequestMapping(value = {"image/{path}"})
	public void image(@PathVariable(value="path") String path, HttpServletResponse response){
		try{
			String fileFolder = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL;
			
			Pattern reg = Pattern.compile("^(.*)-(\\w+?)(_(\\d+)x(\\d+))?$",Pattern.CASE_INSENSITIVE);
			Matcher matcher = reg.matcher(path);

			if(matcher.find()) {
		        /*System.out.println("Found value: " + matcher.group(0) );
		        System.out.println("Found value: " + matcher.group(1) );
		        System.out.println("Found value: " + matcher.group(2) );
		        System.out.println("Found value: " + matcher.group(3) );
		        System.out.println("Found value: " + matcher.group(4) );
		        System.out.println("Found value: " + matcher.group(5) );*/
		        
		        String folder = matcher.group(1);
		        String ext = matcher.group(2);
		        String size = matcher.group(3);
		        int width = StringUtils.toInteger(matcher.group(4));
		        int height = StringUtils.toInteger(matcher.group(5));
		        
		        String foldPath = folder.replace("-", "/");
		        String orignFile = "";
		        String targetFile = "";
		        
		        orignFile = fileFolder +"/"+ foldPath +  "." + ext;
		        
		        if(!StringUtils.isEmpty(size)){
		        	targetFile = fileFolder +"/"+ foldPath + size + "." + ext;
		        }else{
		        	targetFile = fileFolder +"/"+ foldPath +  "." + ext;
		        }
		        
		        File target = new File(targetFile);
		        
		        if(!target.exists()){
		        	ImageUtils.clipBySize(new File(orignFile), new File(targetFile), width, height, true, 1.0f);
		        }
		        
		        response.setContentType("image/*"); 		
		 		FileInputStream fis = null; 		
		 		OutputStream os = null; 		
		 		try { 			
		 			fis = new FileInputStream(targetFile); 			
		 			os = response.getOutputStream();
		 			
		 			int count = 0; 			
		 			byte[] buffer = new byte[1024 * 8]; 			
		 			while ((count = fis.read(buffer)) != -1) { 				
		 				os.write(buffer, 0, count); 				
		 				os.flush(); 			
		 			} 		
		 		} catch (Exception e) { 			
		 			e.printStackTrace(); 		
		 		}
		 		
		 		try { 			
		 			fis.close(); 			
		 			os.close(); 		
		 		} catch (IOException e) { 			
		 			e.printStackTrace(); 		
		 		} 		
			} else {
				//System.out.println("NO MATCH");
				response.setStatus(HttpServletResponse.SC_NOT_FOUND);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error("获取图片异常：",ex);
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}
	}
}